/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taskmanager2ndedition.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author denaas
 */
public class Server implements Runnable {

    private ServerSocket socket;    
    private List<ServerConnection> connections;
    
    public Server(int port) throws IOException {
        this.socket = new ServerSocket(port);
        connections = new ArrayList<ServerConnection>();
    }
    
    public List<ServerConnection> getConnections(){
        return connections;
    }
    
    public void run() {
        while (true) {
            try {
                Socket clientSocket = socket.accept();
                ServerConnection serverConnection = new ServerConnection(clientSocket, this);
                Thread thread = new Thread(serverConnection);
                connections.add(serverConnection);
                thread.start();
            } catch (IOException ex) {
                Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
            }    
        }
    }
}
